var ctx=window.daijia.ctx;

$(document).ready(function(){
	
	$("#start").datetimepicker({ format: 'yyyy-mm-dd', language: 'zh-CN', minView: 'month', pickDate: true, pickTime: false, inputMask: false, autoclose: true, startDate: new Date()});
	$("#end").datetimepicker({ format: 'yyyy-mm-dd', language: 'zh-CN', minView: 'month', pickDate: true, pickTime: false, inputMask: false, autoclose: true, startDate: new Date()});

	$('#companyIds').multiselect({
		enableFiltering: true,
	    includeSelectAllOption: true,
	    selectAllText: '全部',
	    selectAllJustVisible: true,
	    maxHeight:300,
	    buttonWidth: '100%',
	    nonSelectedText: '请选择所属公司',
	    nSelectedText: ' - 已选择',
	    numberDisplayed: 4,
	    allSelectedText: '已选择全部'
	});
	
	/*$('#gradeIds').multiselect({
		enableFiltering: true,
	    includeSelectAllOption: true,
	    selectAllText: '全部',
	    selectAllJustVisible: true,
	    buttonWidth: '100%',
	    nonSelectedText: '请选择客户等级',
	    nSelectedText: ' - 已选择',
	    numberDisplayed: 4,
	    allSelectedText: '已选择全部'
	});*/
	
	$('#passengerFromSource').multiselect({
		enableFiltering: true,
	    includeSelectAllOption: true,
	    selectAllText: '全部',
	    selectAllJustVisible: true,
	    maxHeight:300,
	    buttonWidth: '100%',
	    nonSelectedText: '请选择客户注册渠道',
	    nSelectedText: ' - 已选择',
	    numberDisplayed: 4,
	    allSelectedText: '已选择全部'
	});
	
//	
//	$("#activityType").change(function(){
//		$("#cash_div").hide();
//		$(".coupon_div").hide();
//		$("#coupon_add_div").hide();
//		
//		if($(this).val()=="cash"){
//			$("#cash_div").fadeIn(200);
//		}
//		else if($(this).val()=="coupon"){
//			$(".coupon_div").fadeIn(200);
//			$("#coupon_add_div").fadeIn(200);
//		}
//	});
	
	//添加优惠券
	$("#add_coupon_rule").click(function(){
		var element="<div class='form-group coupon_div'>"+
		                    "<label class='col-sm-3 control-label'></label>"+
		                    "<div class='col-sm-3'>"+
		                    	$("#first_coupon_div select").clone().prop("outerHTML")+
		                    "</div>"+
		                    "<label style='float:left;' class='control-label'><em>*</em>数量：</label>"+
		                    "<div class='col-sm-1'>"+
		                    	"<input class='form-control couponAmount' type='number' min='1' value='1'/>"+
		                    "</div>"+
		                    "<div class='col-sm-1'>"+
		                    	"<button type='button' class='btn btn-default delete_coupon_rule'>"+
								  "<span class='glyphicon glyphicon-remove-sign'></span> 删除"+
								"</button>"+
		                    "</div>"+
	                    "</div>";
		$("#first_coupon_div").after(element);
	});
	
	//删除优惠券
	$(document).on("click",".delete_coupon_rule",function(){
		$(this).parent().parent().remove();
	});
	
	$("#submit_button").click(function(){

		if(main){
			if(null==$("#companyIds").val()){
				$.scojs_message("请选择所属公司", $.scojs_message.TYPE_ERROR);
				return false;
			}
		}
		
		if($("#activityTarget").val()==""){
			$.scojs_message("请选择客户类型", $.scojs_message.TYPE_ERROR);
			return false;
		}

		if($("#bindLine").val()==""){
			$.scojs_message("请选择绑定线路", $.scojs_message.TYPE_ERROR);
			return false;
		}
//		
//		if(null==$("#gradeIds").val()){
//			$.scojs_message("请选择客户等级", $.scojs_message.TYPE_ERROR);
//			return false;
//		}
		
		if(null==$("#passengerFromSource").val()){
			$.scojs_message("请选择客户注册渠道", $.scojs_message.TYPE_ERROR);
			return false;
		}
		
		if($("#activityType").val()==""){
			$.scojs_message("请选择活动类型", $.scojs_message.TYPE_ERROR);
			return false;
		}
		
		if($("#activityType").val()=="cash"){
			var reg=/^(-?\d+)?$/;
			if($("#money").val()=="" || !reg.test($("#money").val())){
		       $.scojs_message('无效的金额！', $.scojs_message.TYPE_ERROR);
		       return false;
		    }
		}
		else if($("#activityType").val()=="coupon"){
			var flag=false;
			var currentSelect;
			$(".couponRule_select").each(function(index,element){
				if($(element).val()==""){
					flag=true;
					currentSelect=element;
					return false;
				}
			})
			if(flag){
				$(currentSelect).focus();
				$.scojs_message('请选择一张优惠券！', $.scojs_message.TYPE_ERROR);
			    return false;
			}
		}
		
		if($("#start").val()==""){
		   $.scojs_message("请选择起始日期", $.scojs_message.TYPE_ERROR);
		   return false;
		}
		
		if($("#end").val()!=""){
			if($("#start").val()>$("#end").val()){
				 $.scojs_message("起始日期不能大于结束日期", $.scojs_message.TYPE_ERROR);
				 return false;
			}
		}
		
//		if($("input[name=sendType]:checked").length==0){
//			$.scojs_message("请选择发送方式", $.scojs_message.TYPE_ERROR);
//			return false;
//		}
		if(main){
			var companyNames=[];
			$("#companyIds option:selected").each(function(index,element){
				companyNames.push($(element).text());
			});
			$("#companyNames").val(companyNames.join(","));
		}
		
		/*var gradeNames=[];
		$("#gradeIds option:selected").each(function(index,element){
			gradeNames.push($(element).text());
		});
		$("#gradeNames").val(gradeNames.join(","));*/
		
		var couponIds=[];
		var couponAmounts=[];
		var eachLimit=[];
		var numerAll=[];
		var lineName = $("#bindLine option:selected").text();
		$(".coupon_div").each(function(index,element){
			couponIds.push($(element).find(".couponRule_select").val());
			couponAmounts.push($(element).find(".couponAmount").val());
			eachLimit.push($(element).find(".couponRule_select option:selected").attr("eachLimit"));
			numerAll.push($(element).find(".couponRule_select option:selected").attr("numerAll"));
		});
		$("#couponIds").val(couponIds.join(","));
		$("#couponAmounts").val(couponAmounts.join(","));

		$("#lineName").val(lineName)

		
		var isRedo = false;
		var s = couponIds.join(",")+","; 
		for(var i=0;i<couponIds.length;i++) { 
			if(s.replace(couponIds[i]+",","").indexOf(couponIds[i]+",")>-1) { 
				isRedo = true;
			}
		}
		if(isRedo){
			$.scojs_message("优惠券重复，请重新选择！", $.scojs_message.TYPE_ERROR);
			return false;
		}
		
		
		for (var i = 0; i < couponAmounts.length; i++) {
			if(parseInt(couponAmounts[i])>10){
				$.scojs_message("优惠券输入数量不能大于10", $.scojs_message.TYPE_ERROR);
				$($(".coupon_div")[i]).find(".couponAmount").focus();
				return false;
			}
			if(parseInt(eachLimit[i])!=0){
				if(parseInt(couponAmounts[i])>parseInt(eachLimit[i])){
					$.scojs_message("该优惠券输入数量不能大于每人限领数量", $.scojs_message.TYPE_ERROR);
					$($(".coupon_div")[i]).find(".couponAmount").focus();
					return false;
				}
			}
			if(parseInt(couponAmounts[i])>parseInt(numerAll[i])){
				$.scojs_message("该优惠券输入数量不能大于优惠券规则总数量", $.scojs_message.TYPE_ERROR);
				$($(".coupon_div")[i]).find(".couponAmount").focus();
				return false;
			}
			
		}
		
		$("#submit_button").attr("disabled",true);

		$.post(ctx+'/activity/create',$("#submit_form").serialize(),function(data){
			if(data.success){
				$.scojs_message("创建成功", $.scojs_message.TYPE_OK);
				window.location=ctx+"/activity/list?event="+$("#activityEvent").val()+"&type="+$("#activityType").val()+"";
			}
			else{
				$.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
				$("#submit_button").attr("disabled",false);
			}
		});
		
	});
	
	


});
